#include "MainComponent.h"
#include "../SimpleDockApplication.h"

#include <vector>

//==============================================================================
MainComponent::MainComponent (RootElement * root_) : root (root_),
													 layoutManager (0),
													 elementToShowText (0),
													 title (0),
													 folderComponent (0)
{	

	rootContainer = new ContainerComponent(0, root);

	addChildComponent(title = new SimpleLabel("ElementTitle"));
	title->setFont(Font(24));

	layoutManager = new LayoutManager(this);
	layoutManager->showContainer(rootContainer);
	layoutManager->doLayout();
	
}

MainComponent::~MainComponent()
{
	deleteAllChildren();

	if(layoutManager)
		delete layoutManager;
}

//==============================================================================
void MainComponent::paint (Graphics& g)
{
	//g.fillAll(Colours::green);
}

void MainComponent::resized()
{
	if(layoutManager)
		layoutManager->doLayout();
}

void MainComponent::mouseDown(const MouseEvent &e)
{
//	(dynamic_cast<SimpleDockApplication*>(JUCEApplication::getInstance()))->getGUIActionsManager()->mouseDownOnApp(e);
	if(e.mods.isRightButtonDown())
	{
		PopupMenu m;
        m.addItem (1, T("Close the Dock"));

        int result = m.showAt (0);

        if (result != 0)
        {
			switch(result)
			{
				case 1:
					JUCEApplication::quit();
					break;
			}
		}
	}
	else
	{
		CircularMenu menu;
			
		Path p;
		p.addArrow(0,0,50,20,2,10,20);
		DrawablePath * edit = new DrawablePath();
		edit->setPath(p);
		DrawablePath * cc = new DrawablePath();
		p.clear();
		p.addBubble(10,10,40,50,4,3,5,0,4,10);
		cc->setPath(p);
		menu.addItem(1, edit, "Copy box");
		menu.addItem(2, cc, "Cut box");
		menu.show(56 /*radius*/); 
	}
}

void MainComponent::showTitle(ElementComponent * element)
{
	if(title)
	{
		elementToShowText = element;
		String text = element->getDockElement()->getTitle();
		title->setText(text, false);
		title->setSize(title->getFont().getStringWidth(text), 20);
		title->setVisible(true);
		resized();
	}
}

void MainComponent::hideTitle()
{
	if(title)
	{
		elementToShowText = 0;
		title->setText(String::empty, false);
		title->setVisible(false);
	}
}

ContainerComponent * MainComponent::getRootContainer()
{
	return rootContainer;
}

void MainComponent::containerVisibilityChanged(ContainerElement * containerElement)
{
	ContainerComponent * cont = findContainerFrom(rootContainer, containerElement);

	if(cont)
	{
		if(cont->isContainerShown())
			layoutManager->hideContainer(cont);
		else
			layoutManager->showContainer(cont);
		resized();
	}
}

void MainComponent::folderVisibilityChanged(FileProcessorElement * element)
{
	if(!folderComponent)
		folderComponent = new FolderComponent(element->getFilePath());
	else
		folderComponent->setPath(element->getFilePath());
	
	addAndMakeVisible(folderComponent);
	folderComponent->setBounds(0,0, 500, 500);
}

void MainComponent::moveElement(ElementComponent * elementToMove, ElementComponent * target)
{
	elementToMove->getParentContainer()->moveElement(elementToMove, target);
}

void MainComponent::removeElement(ElementComponent * element)
{
	//right for now, just linear
	hideTitle();
	element->getParentContainer()->removeElement(element);
}

ContainerComponent * MainComponent::findContainerFrom(ContainerComponent * from, ContainerElement * what)
{
	ContainerComponent * toReturn = 0;

	if(from->getParentElement()->isValidComponent() && from->getParentElement()->getDockElement() == what)
		toReturn = from;

	for(int i = 0; i < from->getNumElements() && toReturn == 0; i++)
	{
		ElementComponent * elem = from->getElementAt(i);
		if(elem->getDockElement()->getType() == container)
		{
			toReturn = findContainerFrom(elem->getChildContainer(), what);
			if(toReturn)
				return toReturn;
		}
	}

	return toReturn;
}


void MainComponent::removeUselessChildContainers(ElementComponent * except)
{
	recursiveRemoveContainers(rootContainer, except);
}

void MainComponent::recursiveRemoveContainers(ContainerComponent * root, ElementComponent * except)
{
}
